

package ru.samgtu.wst.ui.swing.editors;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.ViewFactory;
import javax.swing.text.Element;
import javax.swing.text.View;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTML;
import javax.swing.text.Position;
import javax.swing.text.BadLocationException;

import java.awt.Shape;
import java.awt.Rectangle;
import java.awt.Graphics;


public class MyHTMLEditorKit extends javax.swing.text.html.HTMLEditorKit {
	private static final ViewFactory defaultFactory = new MyHTMLFactory();

	@Override
	public ViewFactory getViewFactory() {
		return defaultFactory;
	}

	private static class MyHTMLFactory extends HTMLEditorKit.HTMLFactory {
		@Override
		public View create(Element elem) {
			Object o = elem.getAttributes().getAttribute(
					StyleConstants.NameAttribute);
			if (o instanceof HTML.Tag) {
				HTML.Tag kind = (HTML.Tag) o;
				if ((kind == HTML.Tag.FRAME) || (kind == HTML.Tag.FRAMESET)
						|| (kind == HTML.Tag.OBJECT) || (kind == HTML.Tag.MAP)
						|| (kind == HTML.Tag.IMG) || (kind == HTML.Tag.LINK)
						|| (kind == HTML.Tag.SCRIPT)
						|| (kind == HTML.Tag.APPLET)) {
					return new NoView(elem);
				}
			}
			return super.create(elem);
		}
	}

	private static class NoView extends View {
		public NoView(Element elem) {
			super(elem);
			setSize(0.0f, 0.0f);
		}

		@Override
		public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
			return 0;
		}

		@Override
		public Shape modelToView(int pos, Shape a, Position.Bias b)
				throws BadLocationException {
			return new Rectangle(0, 0);
		}

		@Override
		public float getPreferredSpan(int axis) {
			return 0.0f;
		}

		@Override
		public void paint(Graphics g, Shape allocation) {
		}
	}

}
